package com.vichat.task;

import com.alibaba.dubbo.container.spring.SpringContainer;
import com.alibaba.fastjson.JSONObject;
import com.vichat.common.cache.RedisHelper;
import com.vichat.common.constant.GlobalConstant;
import com.vichat.common.util.DateUtil;
import com.vichat.common.util.NumberUtils;
import com.vichat.online.entity.StatOnlineUser;
import com.vichat.online.service.IOnlineService;
import com.vichat.res.entity.RoomMicRecordDLog;
import com.vichat.res.service.IResService;
import com.vichat.task.service.ITask;
import com.vichat.user.entity.UserBasic;
import com.vichat.user.service.IUserService;
import com.vichat.utils.OnlineUtil;
import com.vichat.utils.ResUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import redis.clients.jedis.ScanResult;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by ranjx on 2018/06/29.
 * 处理人员在线数据
 */
@Component("ProcessRoomMicRecordTask")
public class ProcessRoomMicRecordTask implements ITask {
    private static transient final Logger logger = LoggerFactory.getLogger(ProcessRoomMicRecordTask.class);


    @Override
    public String doTask(long cfgTaskId) throws Exception {

        try {
            ApplicationContext ctx = SpringContainer.getContext();
            IUserService userService = (IUserService) ctx.getBean("userService", IUserService.class);
            IResService resService = (IResService) ctx.getBean("resService", IResService.class);

            RoomMicRecordReaderThread roomMicRecordReaderThread = new RoomMicRecordReaderThread(resService, userService);
            roomMicRecordReaderThread.start();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    class RoomMicRecordReaderThread extends Thread {
        IResService resService;
        IUserService userService;

        RoomMicRecordReaderThread(IResService resService, IUserService userService) {
            this.resService = resService;
            this.userService = userService;
        }

        public void run() {
            while (true) {
                try {
                    String result;// =  jedis.hscan("hash",DATASOURCE_SELECT);
                    long cursor = 0;
                    do {
                        result = RedisHelper.lindex("MICSTATE", 0);
                        cursor = RedisHelper.llen("MICSTATE");
                        if (result != null) {
                            try {
                                JSONObject json = JSONObject.parseObject(result);
                                long uid = json.getLongValue("uid");
                                RoomMicRecordDealThread thread = new RoomMicRecordDealThread(resService, userService, json);
                                ResUtil.startRoomMicRecordDealer(thread, uid % 10);
                                RedisHelper.lpop("MICSTATE");//刪除
                            } catch (Exception e) {
                                logger.error("Parse MIC_STATE Data Error", e);
                            }
                        }
                    }
                    while (cursor != 0);
                    Thread.sleep(1000 * 5);//处理完毕休息5S
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("Redis hsan {MICSTATE} Error", e);
                }
            }
        }
    }


    class RoomMicRecordDealThread extends Thread {
        IResService resService;
        IUserService userService;
        JSONObject ustate;

        RoomMicRecordDealThread(IResService resService, IUserService userService, JSONObject json) {
            this.resService = resService;
            this.userService = userService;
            this.ustate = json;
        }

        public void run() {

            try {
                long uid = ustate.getLongValue("uid");
                long rid = ustate.getLongValue("rid");
                long startTime = ustate.getLongValue("start");
                long endTime = ustate.getLongValue("stop");

                UserBasic userBasicFDB = userService.getUserBasicByUid(uid);
                if (userBasicFDB != null) {
                    RoomMicRecordDLog record = new RoomMicRecordDLog();
                    record.setUid(uid);
                    record.setPartyId(userBasicFDB.getPartyId());
                    record.setRid(rid);
                    record.setStartTime(new Date(startTime * 1000));
                    record.setEndTime(new Date(endTime * 1000));
                    resService.processRoomMicRecord(record);
                }

            } catch (Exception e) {
                logger.error("Deal Room Mic Record Error", e);
            }

        }
    }

}
